package com.qf.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.shop.entity.OrderAddress;
import com.qf.shop.entity.Orders;
import com.qf.shop.entity.Pay;
import com.qf.shop.mapper.OrdAddressMapper;
import com.qf.shop.mapper.OrdersMapper;
import com.qf.shop.model.TableData;
import com.qf.shop.pojo.QueryOrder;
import com.qf.shop.service.IOrdersService;
import com.qf.shop.vo.OrdAddreeSon;
import com.qf.shop.vo.ChooseAddress;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrdersService {

    private OrdersMapper ordersMapper;
    private OrdAddressMapper ordAddressMapper;

    public OrderServiceImpl(OrdersMapper ordersMapper, OrdAddressMapper ordAddressMapper) {
        this.ordersMapper = ordersMapper;
        this.ordAddressMapper = ordAddressMapper;
    }

    @Override
    public TableData<Orders> getAllOrders(QueryOrder queryOrder, Integer offset, Integer limit) {

        String orderNumber = queryOrder.getOrderNumber();
        if(null != orderNumber && !"".equals(orderNumber.trim())) {
            queryOrder.setOrderNumber("%" + orderNumber + "%");
        }

        String getGoods = queryOrder.getGoods();
        if(null != getGoods && !"".equals(getGoods.trim())) {
            queryOrder.setGoods("%" + getGoods + "%");

        }

        Page<Orders> page = PageHelper.offsetPage(offset, limit);

        ordersMapper.getAllOrder(queryOrder);

        TableData<Orders> tableData = new TableData<>();
        tableData.setRows(page.getResult());
        tableData.setTotal(page.getTotal());

        return tableData;
    }

    @Override
    public Orders getOrderUserById(Integer id) {
        Orders orderUserById = ordersMapper.getOrderUserById(id);
        return orderUserById;
    }

    @Override
    public List<ChooseAddress> getChooseAddress(Integer id) {
        List<ChooseAddress> chooseAddress = ordersMapper.getChooseAddress(id);
        System.out.println(chooseAddress.toString());
        return chooseAddress;
    }

    @Override
    public void changeAddress(Integer aid, Integer uid) {
        ordersMapper.changeAddress(aid, uid);
    }

    @Override
    public List<OrdAddreeSon> showTakeDeliveryAddress(Integer id) {
        List<OrdAddreeSon> list=ordAddressMapper.showTakeDeliveryAddress(id);
        return list;
    }
//
//    @Override
//    public List<OrderAddress> showTakeDeliveryAddress(Integer id) {
//        List<OrderAddress> list=ordAddressMapper.showTakeDeliveryAddress(id);
//        return list;
//    }


}
